package leetcode;

import java.util.Stack;

public class ValidPalentness {

    // "([)]" 应该是false, 但是返回true
    public boolean isValidWrong(String s) {
        Stack<Character> smallLeft = new Stack<>();
        Stack<Character> middleLeft = new Stack<>();
        Stack<Character> largeLeft = new Stack<>();

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(') {
                smallLeft.push(chars[i]);
            } else if (chars[i] == ')') {
                if (smallLeft.empty()) {
                    return false;
                }
                smallLeft.pop();
            } else if (chars[i] == '[') {
                middleLeft.push(chars[i]);
            } else if (chars[i] == ']') {
                if (middleLeft.empty()) {
                    return false;
                }
                middleLeft.pop();
            } else if (chars[i] == '{') {
                largeLeft.push(chars[i]);
            } else if (chars[i] == '}') {
                if (largeLeft.empty()) {
                    return false;
                }
                largeLeft.pop();
            } else {
                return false;
            }
        }
        return smallLeft.empty() && middleLeft.empty() && largeLeft.empty();
    }


    public boolean isValid(String s) {
        Stack<Character> left = new Stack<>();

        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '(' || chars[i] == '[' || chars[i] == '{') {
                left.push(chars[i]);
            } else if (chars[i] == ')') {
                if (left.empty() || left.peek() != '(') {
                    return false;
                }
                left.pop();
            } else if (chars[i] == ']') {
                if (left.empty() || left.peek() != '[') {
                    return false;
                }
                left.pop();
            } else if (chars[i] == '}') {
                if (left.empty() || left.peek() != '{') {
                    return false;
                }
                left.pop();
            } else {
                return false;
            }
        }
        return left.empty();
    }
}
